package jp.michi.makimono;

import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import android.content.Context;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class FolderChooser implements OnItemClickListener {
    protected Context mContext;
    protected TextView mHeader;
    protected ListView mListView;
    protected LinearLayout mContainer;
    protected ArrayAdapter<Folder> mArrayAdapter;
    public FolderChooser(final Context context) {
        mContext = context;
        mHeader = new TextView(context);
        set(mHeader, "long press to select");
        mHeader.setGravity(Gravity.CENTER);
        mListView = new ListView(context);
        mListView.setHeaderDividersEnabled(true);
        mListView.setFooterDividersEnabled(true);
        mListView.setDividerHeight(2);
        mArrayAdapter = new ArrayAdapter<Folder>(context, android.R.layout.simple_list_item_1, list(Preference.getLastVisited(mContext))) {
            @Override
            @SuppressWarnings("unused")
            public View getView(int position, View convertView, ViewGroup parent) {
                return set(new TextView(context), mArrayAdapter.getItem(position).name);
            }
        };
        mListView.setAdapter(mArrayAdapter);
        mListView.setOnItemClickListener(this);
        mContainer = new LinearLayout(context);
        mContainer.setOrientation(LinearLayout.VERTICAL);
        mContainer.addView(mHeader, LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
        mContainer.addView(mListView, LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
    }
    protected static TextView set(TextView textView, CharSequence text) {
        textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 30);
        textView.setText(text);
        return textView;
    }
    @Override
    @SuppressWarnings("unused")
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Folder folder = mArrayAdapter.getItem(position);
        List<Folder> list = list(folder.file);
        if (thereAreFolders(list)) {
            mArrayAdapter.setNotifyOnChange(false);
            mArrayAdapter.clear();
            for (Folder f : list) {
                mArrayAdapter.add(f);
            }
            mArrayAdapter.notifyDataSetChanged();
            mListView.setSelectionAfterHeaderView();
            Preference.setLastVisited(mContext, folder.file.getAbsolutePath());
        }
    }
    protected boolean thereAreFolders(List<Folder> list) {
        return (list.size() > 1);
    }
    protected List<Folder> list(File file) {
        File[] dirs = file.listFiles(new FileFilter() {
            @Override
            public boolean accept(File f) {
                return f.isDirectory();
            }
        });
        List<Folder> list = new ArrayList<Folder>();
        File parentDir = file.getParentFile();
        if (parentDir != null) {
            list.add(new Folder(true, "..", parentDir));
        }
        if (dirs != null && dirs.length > 0) {
            Arrays.sort(dirs);
            for (File f : dirs) {
                list.add(new Folder(false, f.getName(), f));
            }
        }
        return list;
    }
    protected static class Folder {
        boolean parent = false;
        String name;
        File file;
        public Folder(boolean parent, String name, File file) {
            this.parent = parent;
            this.name = name;
            this.file = file;
        }
        @Override
        public String toString() {
            return name;
        }
    }
}
